package luanvan.gps;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class GPSTracke extends Service implements LocationListener {

	private final Context mContext;
	boolean isGPSEnable = false;
	boolean isNetworkEnale = false;
	public boolean canGetLocation = false;
	public Location location=null;
	public double latitude, longitde;
	
	private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE =10;// met
	private static final long MIN_TIME_BW_UPDATES = 1000*60*1; // 1 minute
	
	
	
	public GPSTracke(Context _mContext) {
		super();
		this.mContext = _mContext;
		getLocation();
	}
	
	public Location getLocation(){
		try{
			locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 
			isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			isNetworkEnale = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			if(!isGPSEnable && !isNetworkEnale){
				Toast.makeText(mContext, "you must enable gps or connect network to use service gps", Toast.LENGTH_LONG).show();
			}else{
				this.canGetLocation = true;
				if(isNetworkEnale){
					locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
					Log.d("network", "network");
					if(locationManager!=null){
						location= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if(location!=null){
							latitude= location.getLatitude();
							longitde = location.getLongitude();
						}
					}
				}
				if(isGPSEnable){
					if(location==null){
						locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
						Log.d("GPS Enable", "GPS Enable");
						if(locationManager!=null){
							location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if(location!=null)
							{
								latitude = location.getLatitude();
								longitde = location.getLongitude();
							}
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return location;
	}
	
	public void stopUsingGPS(){
		if(locationManager!=null){
			canGetLocation=false;
			location=null;
			locationManager.removeUpdates(GPSTracke.this);
		}
	}
	
	protected LocationManager locationManager;
	
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
